package com.xuxueli.applyModules.utils.testbit;
import org.apache.shiro.crypto.hash.SimpleHash;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class TestRightHelper {

    public static void main(String[] args) {
        List<TestBitPara> list = new ArrayList<>();
        List<TestBitPara> subList = new ArrayList<>();
        List<TestBitPara> subLists = new ArrayList<>();

        TestBitPara testBitPara1 = new TestBitPara();
        TestBitPara testBitPara2 = new TestBitPara();
        TestBitPara testBitPara3 = new TestBitPara();
        TestBitPara testBitPara4 = new TestBitPara();
        TestBitPara testBitPara5 = new TestBitPara();
        TestBitPara testBitPara6 = new TestBitPara();

        testBitPara1.setId(1);
        testBitPara1.setName("第一级1");
        testBitPara1.setPid(0);

        testBitPara2.setId(2);
        testBitPara2.setName("第一级2");
        testBitPara2.setPid(0);

        testBitPara3.setId(3);
        testBitPara3.setName("第二级1");
        testBitPara3.setPid(1);

        testBitPara4.setId(4);
        testBitPara4.setName("第二级2");
        testBitPara4.setPid(1);

        testBitPara5.setId(5);
        testBitPara5.setName("第三级1");
        testBitPara5.setPid(3);

        testBitPara6.setId(6);
        testBitPara6.setName("第三级2");
        testBitPara6.setPid(3);

        subLists.add(testBitPara5);
        subLists.add(testBitPara6);
        testBitPara3.setSubList(subLists);

        subList.add(testBitPara3);
        subList.add(testBitPara4);
        testBitPara1.setSubList(subList);

        list.add(testBitPara1);
        list.add(testBitPara2);
        String menuIdss = "1,2,3,4,5";
        BigInteger rightss = RightsHelper.sumRights(str2StrArray(menuIdss));
        boolean testRights = RightsHelper.testRights(rightss, 1);
        System.out.println(testRights);
        List<TestBitPara> list1 = getList(list, rightss.toString());
        System.out.println(list1);
        SimpleHash simpleHash = new SimpleHash("SHA-1", "admin", "123987");
        String value = simpleHash+"te8s8t97";
        String replace = value.replace("te8s8t97", "");
        System.out.println("用户名密码加密1："+value);
        System.out.println("用户名密码加密2："+replace);
        System.out.println("用户名密码解密："+simpleHash.toString().equals(replace));
        //用菜单ID做加密
        String menuIds = "16,18";
        BigInteger rights = RightsHelper.sumRights(str2StrArray(menuIds));
        //解析
        boolean testRight = RightsHelper.testRights(rights, "18");
        System.out.println(String.format("加密后的值=>%s\n解析是否存在=>%s",rights,testRight));
    }

    public static List<TestBitPara> getList(List<TestBitPara> list,String roleRights){
        for (int i = 0; i <list.size() ; i++) {
            System.out.println(RightsHelper.testRights(roleRights, list.get(i).getId()));
            list.get(i).setIsExist((RightsHelper.testRights(roleRights, list.get(i).getId())));
            getList(list.get(i).getSubList(),roleRights);
        }
        return list;
    }

    /**
     * 检测字符串是否为空(null,"","null")
     * @param s
     * @return 为空则返回true，不否则返回false
     */
    public static boolean isEmpty(String s){
        return s==null || "".equals(s) || "null".equals(s);
    }
    /**
     * 字符串转换为字符串数组
     * @param str 字符串
     * @param splitRegex 分隔符
     * @return
     */
    public static String[] str2StrArray(String str,String splitRegex){
        if(isEmpty(str)){
            return null;
        }
        return str.split(splitRegex);
    }

    /**
     * 用默认的分隔符(,)将字符串转换为字符串数组
     * @param str	字符串
     * @return
     */
    public static String[] str2StrArray(String str){
        return str2StrArray(str,",\\s*");
    }


}